package com.hybrid.jsbridge;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import android.content.Context;
import android.content.Intent;
import com.hybrid.webview.WVWebView;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-09 22:23
 * @version: 9.1.0
 */
public class WVPluginEntryManager {
    private Context mContext = null;
    private WVWebView mWebView = null;
    private Map<String, Object> entryMap = new HashMap<String, Object>();
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);

    public WVPluginEntryManager(Context context, WVWebView webView) {
        this.mContext = context;
        this.mWebView = webView;
    }

    public void addEntry(String name, Object instance) {
        lock.writeLock().lock();
        try {
            entryMap.put(name, instance);
        } finally {
            lock.writeLock().unlock();
        }
    }

    public Object getEntry(String name) {
        lock.readLock().lock();
        Object jsObject;
        try {
            jsObject = entryMap.get(name);
        } finally {
            lock.readLock().unlock();
        }
        if (jsObject == null) {
            lock.writeLock().lock();
            try {
                if (entryMap.get(name) == null) {
                    WVApiPlugin plugin = WVPluginManager.createPlugin(name, mContext, mWebView);
                    if (plugin != null) {
                        entryMap.put(name, plugin);
                        jsObject = plugin;
                    }
                }
            } finally {
                lock.writeLock().unlock();
            }
        }
        return jsObject;
    }

    /**
     * webview销毁时通知各个jsbridge进行销毁操作，并最终销毁jsbridge实例
     */
    public void onDestroy() {
        lock.readLock().lock();
        try {
            for (Object value : entryMap.values()) {
                if (value instanceof WVApiPlugin) {
                    ((WVApiPlugin)value).onDestroy();
                }
            }
        } finally {
            lock.readLock().unlock();
        }
        lock.writeLock().lock();
        try {
            entryMap.clear();
        } finally {
            lock.writeLock().unlock();
        }
    }

    public void onPause() {
        lock.readLock().lock();
        try {
            for (Object value : entryMap.values()) {
                if (value instanceof WVApiPlugin) {
                    ((WVApiPlugin)value).onPause();
                }
            }
        } finally {
            lock.readLock().unlock();
        }
    }

    public void onResume() {
        lock.readLock().lock();
        try {
            for (Object value : entryMap.values()) {
                if (value instanceof WVApiPlugin) {
                    ((WVApiPlugin)value).onResume();
                }
            }
        } finally {
            lock.readLock().unlock();
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        lock.readLock().lock();
        try {
            for (Object value : entryMap.values()) {
                if (value instanceof WVApiPlugin) {
                    ((WVApiPlugin)value).onActivityResult(requestCode, resultCode, intent);
                }
            }
        } finally {
            lock.readLock().unlock();
        }
    }

    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        lock.readLock().lock();
        try {
            for (Object value : entryMap.values()) {
                if (value instanceof WVApiPlugin) {
                    ((WVApiPlugin)value).onScrollChanged(l, t, oldl, oldt);
                }
            }
        } finally {
            lock.readLock().unlock();
        }
    }
}
